﻿package com.brilliance.project.common.tag;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.brilliance.project.common.Constants;

public class CheckLogonTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	// Log instance
	static Log log = LogFactory.getLog(CheckLogonTag.class);

	/**
	 * description：当解析标签开始时的动作
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	/**
	 * dscription:檢查logon的user是否存在於session中。 <br/>
	 * 否則就forwarded to the specified logon page.<br/>
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doEndTag() throws JspException {

		// Is there a valid user logged on?
		boolean valid = false;
		HttpSession session = pageContext.getSession();
		if ((session != null)
				&& (session.getAttribute(Constants.SESSION_AGENT_KEY)) != null) {
			valid = true;
		}
		if (valid) {
			return (EVAL_PAGE);
		} else {
			try {
				pageContext.forward("/common/timeout.jsp");
			} catch (ServletException e) {
				throw new JspException(e.toString());
			} catch (IOException e) {
				throw new JspException(e.toString());
			}
			return (SKIP_PAGE);
		}
	}

	/**
	 * Release any acquired resources.
	 */
	public void release() {
		super.release();
	}
}
